package com.ego.item.service.impl;

import com.ego.item.mapper.CategoryMapper;
import com.ego.item.pojo.Category;
import com.ego.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 根据parentId查询子类目
     *
     * @param pid
     * @return
     */
    @Override
    public List<Category> queryCategoryListByParentId(Long pid) {
        Category record = new Category();
        record.setParentId(pid);
        return this.categoryMapper.select(record);
    }

    //删除当前目录下面的子目录，用了个递归，丫的，前面用循环使用没写出来得，，，难受
    //本想试试循环滴，发现方法有点笨，浪费了一点时间
    @Override
    public void deleteById(Long id) {
        Example example = new Example(Category.class);
        example.createCriteria().andEqualTo("parentId", id);
        List<Category> categories = categoryMapper.selectByExample(example);
        if (categories != null && categories.size() != 0) {
            for (Category category :
                    categories) {
                deleteById(category.getId());
            }
        } else {
            this.categoryMapper.deleteByPrimaryKey(id);
            //操作一下中间表,分类和品牌之间的关系
            this.categoryMapper.deleteCategoryBrand(id);
            //错做一下中间表,分类和规格说明(specification)之间的关系
            this.categoryMapper.deleteCategorySpecification(id);
        }
    }

    @Override
    public void addCategory(Category category) {
        //判断下当前要添加目录的父目录是不是parent级的目录
        Category parent = this.categoryMapper.selectByPrimaryKey(category.getParentId());
        if (parent != null) {
            if (!parent.isParent()) {//如果不是父目录的话,我们将它变为父目录级别的
                parent.setIsParent(true);
                this.categoryMapper.updateByPrimaryKeySelective(parent);
            }
        }
        this.categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public List<Category> getByBrandId(Long bid) {

        //因为关系建立在中间表,所以需要操作一个子查询
        List<Category> categories = categoryMapper.getListByBrandId(bid);
        return categories;
    }

    @Override
    public List<String> queryNameByIds(List<Long> asList) {
        List<Category> categories = categoryMapper.selectByIdList(asList);
        List<String> names = new ArrayList(categories.size());
        for (Category category :
                categories) {
            names.add(category.getName());
        }
        return names;
    }

    @Override
    public List<Category> queryCategoryListByIds(List<Long> categoryIds) {
        if (!CollectionUtils.isEmpty(categoryIds)) {
            return this.categoryMapper.selectByIdList(categoryIds);
        } else {
            return null;
        }
    }

    @Override
    public List<Category> queryAllCategoryLevelByCid3(Long id) {
        List<Category> categories = new ArrayList<>();
        Category category = categoryMapper.selectByPrimaryKey(id);
        while (category != null && category.getParentId() != 0) {
            categories.add(category);
            category = categoryMapper.selectByPrimaryKey(category.getParentId());
        }
        categories.add(category);
        return categories;
    }

}
